/*
    <one line to give the library's name and an idea of what it does.>
    Copyright (C) 2011  Admin <admin@tnpconsultants.net>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#ifndef  SPRITEANIMATE_HPP
# define SPRITEANIMATE_HPP

# include <SFML/Graphics.hpp>

namespace Shiny
{
  class SpriteAnimate : public sf::Sprite
  {
  public:
    class Animation
    {
    public:
      Animation(void);
      Animation(sf::Texture*);
      Animation(const Animation&);
      Animation(sf::Texture*, sf::IntRect, unsigned short nFrames, float refresh, bool inLoop = true);

      void                     SetRefreshRate(float);
      void                     SetInLoop(bool);
      void                     operator()(SpriteAnimate*);
      void                     SetNextFrame(SpriteAnimate*);
      void                     Reset(void);
      sf::Texture*             Texture(void);
      sf::Vector2f             Size(void) const;
      const sf::IntRect&       Rect(void) const { return (_rect); }
      void                     SetRect(const sf::IntRect& r) { _rect = r;    }
      void                     SetTexture(sf::Texture* t)    { _image = t;   }
      void                     SetNFrames(unsigned short i)  { _nFrames = i; }
      float                    NFrames(void)const      { return (_nFrames);     }
      bool                     IsInLoop(void) const    { return (_inLoop);      }
      float                    RefreshRate(void) const { return (_refreshRate); }

    private:
      sf::Texture*             _image;
      sf::IntRect              _rect;
      sf::Clock                _clock;
      unsigned short           _currentFrame;
      unsigned short           _nFrames;
      bool                     _inLoop;
      float                    _refreshRate;
    };

    class FunctorDraw
    {
    public:
      FunctorDraw(sf::RenderTarget& target) : _target(target) {}
      void            operator()(SpriteAnimate* object)
      {
    object->Update();
        object->Draw(_target);
      }

    private:
      sf::RenderTarget& _target;
    };

    typedef std::vector<Animation> AnimationArray;

    SpriteAnimate(void);
    SpriteAnimate(const AnimationArray&);
    void SetAnimations(const AnimationArray&);
    void SetAnimation(unsigned int);
    void Update(void);

    virtual void Draw(sf::RenderTarget& target)
    {
      target.Draw(*this);
    }

    virtual void SetTextureRect(const sf::IntRect& aabb);

  private:
    AnimationArray _animations;
    unsigned int   _currentAnimation;
  };
}

#endif // SPRITEANIMATE_HPP
